#include "ResourceManager.h"

ResourceManager* ResourceManager::m_resource = NULL;

ResourceManager* ResourceManager::getInstance()
{
	if (m_resource == NULL)
	{
		m_resource = new ResourceManager();
	}
	return m_resource;
}

void ResourceManager::prepareManager(LPD3DXSPRITE sp, LPDIRECT3DDEVICE9 pd3Device, HINSTANCE Hinst, HWND wndHandle, 
	CInput *input, CTexture *texture, CAudio *audio)
{
	getInstance()->m_pd3Device = pd3Device;
	getInstance()->m_Hinst = Hinst;
	getInstance()->m_wndHandle = wndHandle;
	getInstance()->m_input = input;
	getInstance()->m_texture = texture;
	getInstance()->m_spriteHandler = sp;
	getInstance()->m_audio = audio;	
}


ResourceManager::ResourceManager()
{

}

ResourceManager::~ResourceManager()
{
}

/* -------------- Menu Scene -------------*/
void ResourceManager::loadMenuResource()
{
	loadMenuAudio();
	loadMenuGraphics();
}

void ResourceManager::loadMenuGraphics()
{
	menu_bg = new CSprite(194,207);
	menu_bg->init(m_texture->getTexture(PATH_MENU_BG, D3DCOLOR_XRGB(0, 0, 0)));

	menu_badge = new CSprite(16,10);
	menu_badge->init((m_texture->getTexture(PATH_MENU_BADGE, D3DCOLOR_XRGB(0, 0, 0))));

	menu_item1 = new CSprite(64,14);
	menu_item1->init((m_texture->getTexture(PATH_MENU_ITEM1, D3DCOLOR_XRGB(0, 0, 0))));

	menu_item2 = new CSprite(72,7);
	menu_item2->init((m_texture->getTexture(PATH_MENU_ITEM2, D3DCOLOR_XRGB(0, 0, 0))));

}

void ResourceManager::loadMenuAudio()
{
	music_menu_bg = m_audio->LoadSound(PATH_MUSIC_MENU_BG);
}


void ResourceManager::unloadMenuResource()
{

}

/* -------------- Loading Scene -------------*/
void ResourceManager::loadLoadingResource()
{
	loadLoadingGraphics();
	loadLoadingAudio();
}

void ResourceManager::loadLoadingGraphics()
{
	load_1p = new CSprite(16, 10);
	load_1p->init(m_texture->getTexture(PATH_LOAD_1P, D3DCOLOR_XRGB(0, 0, 0)));

	load_rest = new CSprite(32, 11);
	load_rest->init(m_texture->getTexture(PATH_LOAD_REST, D3DCOLOR_XRGB(0, 0, 0)));

	load_hi = new CSprite(16, 10);
	load_hi->init(m_texture->getTexture(PATH_LOAD_HI, D3DCOLOR_XRGB(0, 0, 0)));

	load_stage = new CSprite(50, 10);
	load_stage->init(m_texture->getTexture(PATH_LOAD_STAGE, D3DCOLOR_XRGB(0, 0, 0)));

	load_nameStage = new CSprite(49, 11);
	load_nameStage->init(m_texture->getTexture(PATH_LOAD_NAMESTAGE, D3DCOLOR_XRGB(0, 0, 0)));

	load_number = new CSprite(40, 10);
	load_number->init(m_texture->getTexture(PATH_LOAD_NUMBER, D3DCOLOR_XRGB(0, 0, 0)));
}

void ResourceManager::loadLoadingAudio()
{

}

void ResourceManager::unloadLoadingResource()
{

}


/* -------------- Play Scene -------------*/
void ResourceManager::loadPlayResource()
{
	
	switch (m_levelMap)
	{
	case 1:
		loadMap1();
		break;
	case 2:
		break;
	default:
		break;
	}

	loadPlayGraphics();
	loadPlayAudio();
}

void ResourceManager::loadPlayGraphics()
{
	// HUD
	HUD_badge = new CSprite(8, 16);
	HUD_badge->init(m_texture->getTexture(PATH_HUD_BADGE, D3DCOLOR_XRGB(255, 255, 255)));	

	// Object
	cS_character = new CSprite(216, 276, 36, 6);
	cS_character->init(m_texture->getTexture(PATH_OB_CHARACTER, D3DCOLOR_XRGB(255, 0, 255)));
	cS_character->setIndex(36);

	cS_ground = new CSprite(32,16);
	cS_ground->init(m_texture->getTexture(PATH_OB_GROUND, D3DCOLOR_XRGB(255, 255, 255)));

}


void ResourceManager::loadPlayAudio()
{

}

void ResourceManager::loadMap1()
{

	// load background
	play_bg1 = new CSprite(256, 128);
	play_bg1->init(m_texture->getTexture(PATH_MAP_BG1_PIC, D3DCOLOR_XRGB(0, 0, 0)));

	map1_listTile = loadFileTextBG(PATH_MAP_BG1_TEXT);
	

	// load object
	map1_listOb = loadFileTextOB(PATH_OB_MAP1);

	// load camera + character
	float posX = map1_listOb.at(map1_listOb.size()-2)->getPosX();
	float posY = map1_listOb.at(map1_listOb.size()-2)->getPosY();

	m_camera = new CCamera(posX,posY);

	posX = map1_listOb.at(map1_listOb.size()-1)->getPosX();
	posY = map1_listOb.at(map1_listOb.size()-1)->getPosY();

	// xoa ob cuoi cua list
	m_posChar = D3DXVECTOR2(posX,posY);	map1_listOb.erase(map1_listOb.end()-2, map1_listOb.end());	

	// load quadtree
	map1_listNode =	loadFileTextNode(PATH_QT_MAP1);

	// load object	
	cS_obstacle = new CSprite(170, 100,15,5);
	cS_obstacle->init(m_texture->getTexture(PATH_OB_OBSTACLE, D3DCOLOR_XRGB(0, 0, 0)));
	cS_sniper = new CSprite(180,40,5,5);
	cS_sniper->init(m_texture->getTexture(PATH_OB_SNIPER, D3DCOLOR_XRGB(255, 0, 255)));
	cS_bragdeEagle = new CSprite(238, 32, 7, 7);
	cS_bragdeEagle->init(m_texture->getTexture(PATH_OB_BRAGDEAEGLE));
	cS_blockhouse = new CSprite(102, 168, 15, 3);
	cS_blockhouse->init(m_texture->getTexture(PATH_OB_BLOCKHOUSE, D3DCOLOR_XRGB(255, 0, 255)));
	cS_runman = new CSprite(120, 33, 6, 6);
	cS_runman->init(m_texture->getTexture(PATH_OB_RUNMAN, D3DCOLOR_XRGB(255, 0,255)));
	cS_item = new CSprite(270, 15, 10, 10);
	cS_item->init(m_texture->getTexture(PATH_OB_ITEM, D3DCOLOR_XRGB(255, 0, 255)));

}

void ResourceManager::loadMap2()
{
	// load background
	play_bg2 = new CSprite(256, 144);
	play_bg2->init(m_texture->getTexture(PATH_MAP_BG2_PIC, D3DCOLOR_XRGB(0, 0, 0)));
	map2_listTile = loadFileTextBG(PATH_MAP_BG2_TEXT);
}


void ResourceManager::unloadPlayResource()
{

}

vector<int> ResourceManager::loadFileTextBG(string path)
{
	vector<int> x;
	ifstream in(path, ios::in);
	int number;
	while (in >> number)
	{
		x.push_back(number);
	}

	in.close();
	return x;
}
vector<QTNode*> ResourceManager::loadFileTextNode(string path)
{
	vector<QTNode*> listNode;
	string line;
	ifstream myfile(path);
	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			QTNode *ob = splitTextPushNode(line);
			listNode.push_back(ob);
		}
		myfile.close();
	}
	return listNode;
}

vector<GameObject*> ResourceManager::loadFileTextOB(string path)
{
	vector<GameObject*> gameOb;
	string line;
	ifstream myfile(path);
	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			GameObject *ob = splitTextPushOB(line);
			
			gameOb.push_back(ob);
		}
		myfile.close();
	}
	return gameOb;
}

GameObject* ResourceManager::splitTextPushOB(string str)
{
	GameObject* ob = new GameObject();	
	string delimiter = " ";
	int dem = 0;
	size_t pos = 0;
	std::string token;
	while ((pos = str.find(delimiter)) != std::string::npos) {
		token = str.substr(0, pos);
		if (dem==0)
		{			
			ob->setID(atoi(token.c_str()));
		}
		else if (dem == 1)
		{
			ob->setGameObID(token);
		}
		else if (dem == 2)
		{
			ob->setPosX(atoi(token.c_str()));
		}
		else if (dem == 3)
		{
			ob->setPosY(atoi(token.c_str()));
		}	
		else if (dem == 4)
		{
			ob->setWidth(atoi(token.c_str()));
		}
		else if (dem == 5)
		{
			ob->setHeight(atoi(token.c_str()));
		}
		str.erase(0, pos + delimiter.length());
		dem++;
	}

	return ob;
}


QTNode* ResourceManager::splitTextPushNode(string str)
{
	QTNode* node = new QTNode();
	string delimiter = " ";
	int dem = 0;
	size_t pos = 0;
	std::string token;
	while ((pos = str.find(delimiter)) != std::string::npos) {
		token = str.substr(0, pos);
		if (dem == 0)
		{
			node->setID(token);
			int lv = token.length() / 2;
			node->setLevel(lv);
		}
		else if (dem == 1)
		{
			node->setPosX(atoi(token.c_str()));
		}
		else if (dem == 2)
		{
			node->setPosY(atoi(token.c_str()));
		}
		else if (dem == 3)
		{
			node->setWidth(atoi(token.c_str()));
		}
		else if (dem == 4)
		{
			node->setHeight(atoi(token.c_str()));
		}
		else if (dem == 5)
		{
			// truong hop co list object o node la			
			token.erase(pos, pos + delimiter.length());			
			node->setListOb(token);
		}

		str.erase(0, pos + delimiter.length());
		dem++;
	}

	return node;
}